﻿using MvcApplication1.Domain;
using MvcApplication1.Models.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace MvcApplication1.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        public static Dictionary<String, User> loginUserList = new Dictionary<String, User>();

        public ActionResult Index()
        {
            return View("Login");
        }

        [HttpPost]
        public ActionResult Index(LogIn model)
        {
            if(ModelState.IsValid) {
                if(loginUserList.ContainsKey(model.UserName)) {
                    if(loginUserList[model.UserName].pass.Equals(model.Password)) {
                        FormsAuthentication.SetAuthCookie(model.UserName, model.rememberMe);
                        Session.Add("UserModel", model);
                        Session["CurrentUser"] = model;
                        loginUserList[model.UserName].loginStatus = true;
                        return RedirectToAction("","Kabin1");
                    }
                }
                ModelState.AddModelError("", "Username or password is wrong");
            }

            return View("Login");
        }

        public ActionResult Logout()
        {
            Session.Clear();
            FormsAuthentication.SignOut();
            return RedirectToAction("", "Login");
        }

    }
}
